#ifndef EEPROM_H
#define EEPROM_H

#include <stdint.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

/* I2C总线ioctl方法所使用的结构体 */
typedef struct i2c_rdwr_ioctl_data ioctl_st;

/* eeprom的slave_address, 对于AT24C08, 容量为1024 bytes, 由于byte_address
   仅有8位, 只能表示0 ~ 255 bytes, 故此需在slave_address中分出2位来配合
   byte_address一起寻址, 而slave_address可以取值0x50, 0x51, 0x52, 0x53,
   配合byte_address刚好可以实现0 ~ 1023 bytes的寻址, 另外byte_address
   取值范围从0 ~ 255, 若大于255则按其值对256取余计算 */
enum eeprom_address {
    eeprom_addr0 = 0x54,
    eeprom_addr1 = 0x55,
    eeprom_addr2 = 0x56,
    eeprom_addr3 = 0x57,
};


#define EEPROM_I2C_DEV_PATH				"/dev/i2c-4"
#define I2C_M_WR          				0    //定义写标志
#define MAX_MSG_NR        				2    //根据AT24C08手册, 最大消息数为2
#define EEPROM_BLOCK_SIZE 				256  //每个block容量256 bytes
#define EEPROM_PAGE_SIZE  				16   //AT24C08页大小为16字节
#define I2C_MSG_SIZE 					(sizeof(struct i2c_msg))


#define EE_PROG_MARK_OFFSET				0xFB

#define EE_DEV_SN_OFFSET				0x00
#define EE_MANUF_DATA_OFFSET			0x10
#define EE_HW_VERSION_OFFSET			0x18
#define EE_FW_VERSION_OFFSET			0x1B
#define EE_UBOOT_VERSION_OFFSET			0x20
#define EE_KERNEL_VERSION_OFFSET		0x24
#define EE_ROOTFS_VERSION_OFFSET		0x28
#define EE_SW_VERSION_OFFSET			0x2B
#define EE_MAC_ADDER_OFFSET				0x30
#define EE_IP_ADDER_OFFSET				0x40

#define EE_CH0_CALIB_VAL_OFFSET			0x50
#define EE_CH1_CALIB_VAL_OFFSET			0x58
#define EE_CH2_CALIB_VAL_OFFSET			0x60
#define EE_CH3_CALIB_VAL_OFFSET			0x68
#define EE_CH4_CALIB_VAL_OFFSET			0x70
#define EE_CH5_CALIB_VAL_OFFSET			0x78
#define EE_CH6_CALIB_VAL_OFFSET			0x80
#define EE_CH7_CALIB_VAL_OFFSET			0x88
#define EE_CH8_CALIB_VAL_OFFSET			0x90
#define EE_CH9_CALIB_VAL_OFFSET			0x98
#define EE_CH10_CALIB_VAL_OFFSET		0xA0
#define EE_CH11_CALIB_VAL_OFFSET		0xA8
#define EE_CH12_CALIB_VAL_OFFSET		0xB0
#define EE_CH13_CALIB_VAL_OFFSET		0xB8
#define EE_CH14_CALIB_VAL_OFFSET		0xC0
#define EE_CH15_CALIB_VAL_OFFSET		0xC8

#define EE_TEMP0_CALIB_VAL_OFFSET		0xD0
#define EE_TEMP1_CALIB_VAL_OFFSET		0xD8
#define EE_TEMP2_CALIB_VAL_OFFSET		0xE0
#define EE_TEMP3_CALIB_VAL_OFFSET		0xE8



/* 自定义eeprom参数结构体 */
typedef struct eeprom_data {
    uint8_t slave_addr;
    uint8_t byte_addr;
    uint8_t len;
    uint8_t *buf;
} eeprom_st;


/* 从eeprom的pos位置处读取1个字节
   @pos: eeprom的byte_address, 取值范围为0 ~ 255
   返回值为读取的字节数据 */
extern uint8_t eeprom_byte_read(uint8_t addr, uint8_t pos);

/* 将1个字节数据写入eeprom的pos位置
   @pos: eeprom的byte_address, 取值范围为0 ~ 255
   @data: 待写入的字节数据 */
extern int eeprom_byte_write(uint8_t addr, uint8_t pos, uint8_t data);

/* 从eeprom的pos位置开始读取size长度数据
   @pos: eeprom的byte_address, 取值范围为0 ~ 255
   @data: 接收数据的缓冲区
   @size: 待读取的数据长度, 取值范围为1 ~ 16 */
extern int eeprom_page_read(uint8_t addr, uint8_t pos, uint8_t *data, int size);

/* 自eeprom的pos位置开始写入数据
   @pos: eeprom的byte_address, 取值范围为0 ~ 255
   @data: 待写入的数据
   @size: 待写入数据的长度, 取值范围为1 ~ 16 */
extern int eeprom_page_write(uint8_t addr, uint8_t pos, uint8_t *data, int size);

#endif

